package com.xps.purifierapp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xps.purifierapp.R;
import com.xps.purifierapp.bean.LoginBean;
import com.xps.purifierapp.date.net.UserNetWorks;
import com.xps.purifierapp.utils.SharedPreferencesUtils;
import com.xps.purifierapp.view.wigdet.LoadingDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import rx.Subscriber;

/**
 * Created by liwenhan on 2017/11/6.
 */

public class LoginActivity extends CheckPermissionsActivity {

    @Bind(R.id.TextView_UserName)
    EditText TextViewUserName;
    @Bind(R.id.TextView_Password)
    EditText TextViewPassword;
    @Bind(R.id.Button_Login)
    TextView ButtonLogin;
    @Bind(R.id.TextVIEw_ForgetPassword)
    TextView TextVIEwForgetPassword;
    LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        TextVIEwForgetPassword.setVisibility(View.GONE);
        TextViewUserName.setText(SharedPreferencesUtils.getString("userName", "String", null));
        TextViewPassword.setText(SharedPreferencesUtils.getString("userPassword", "String", null));
        loadingDialog = new LoadingDialog(this);
    }

    @OnClick({R.id.Button_Login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Button_Login:
                JCcheckIn();
                loadingDialog.setMessage("正在登录").show();
                break;

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (LoginActivity.this.getCurrentFocus() != null) {
                if (LoginActivity.this.getCurrentFocus().getWindowToken() != null) {
                    imm.hideSoftInputFromWindow(LoginActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
        return super.onTouchEvent(event);
    }

    void JCcheckIn() {
        UserNetWorks.UserLogin(TextViewUserName.getText().toString(), TextViewPassword.getText().toString(), new Subscriber<LoginBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(LoginActivity.this, "请检查网络", Toast.LENGTH_LONG).show();
                e.printStackTrace();
                loadingDialog.dismiss();
            }

            @Override
            public void onNext(LoginBean loginBean) {
                loadingDialog.dismiss();
                if (loginBean.getReturn_code().equals("SUCCESS")) {
                    SharedPreferencesUtils.putString("rel_name", "String", loginBean.getReturn_body().getRel_name());
                    SharedPreferencesUtils.putString("user_token", "String", loginBean.getReturn_body().getUser_token());
                    Log.e("Token", loginBean.getReturn_body().getUser_token());
                    SharedPreferencesUtils.putString("userName", "String", TextViewUserName.getText().toString());
                    SharedPreferencesUtils.putString("userPassword", "String", TextViewPassword.getText().toString());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    JPushInterface.setAliasAndTags(LoginActivity.this, TextViewUserName.getText().toString(), null, null);
                    finish();
                } else if (loginBean.getReturn_code().equals("FALL")) {
                    Toast.makeText(LoginActivity.this, loginBean.getReturn_msg(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
